//****************************************************
//****************************************************
		//Сǿ������СϵͳҺ��ת�Ӱ�����
//****************************************************
//****************************************************
#include "main.h"
#include <stdio.h>
#include <stdlib.h>


typedef unsigned int u16;	  //
typedef unsigned char u8;

//--����ʹ�õ�IO--//
#define GPIO_Dx P1
#define GPIO_csx P0
#define GPIO_Fx P2


//--����ȫ�ֱ���--//
unsigned char code DIG_CODE[64]={
0x00, 0x00, 0xFE, 0x80, 0x80, 0x80, 0x00, 0x00, 
0x00, 0x0C, 0x92, 0x92, 0x92, 0x7C, 0x00, 0x00, 
0x00, 0x00, 0x9C, 0x92, 0x92, 0xF2, 0x00, 0x00, 
0x00, 0x08, 0x08, 0xFE, 0x48, 0x28, 0x18, 0x00, 
0x00, 0x00, 0xFE, 0x92, 0x92, 0x92, 0x00, 0x00, 
0x00, 0x00, 0xF2, 0x92, 0x92, 0x9E, 0x00, 0x00, 
0x00, 0x02, 0x02, 0xFE, 0x42, 0x02, 0x00, 0x00, 
0x00, 0x30, 0x48, 0x44, 0x22, 0x44, 0x48, 0x30, 
};


//****************************************************
//������
//****************************************************
u8 lightT;//������������(1-50)
void main()
{
	
	lightT = 30;
	

	while(1)
	{
//		LED1 = 0;			 //LED1����
//		LED2 = 1;			 //LED2Ϩ��
//		Delay_ms(1000);		 //��ʱ1s
//		LED1 = 1;			 //LED1Ϩ��
//		LED2 = 0;			 //LED2����
//		Delay_ms(1000);		 //��ʱ1s		
		
		DigDisplay();
	}
}


u8 f,d;
u16 t1,t2;

//����������ʹ�õ�����
unsigned char DIG_CODE_0[64]={
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	
};

//************************************����ִ�еĵط�*****************************************************
void DigDisplay()
{
//timeִ�еĴ�����speed�ٶȴ�1��ʼ  order�����Ĭ��0�� ���ݲ��Ǻ�׼���Լ���ȥ�ɡ���

//	flash_3(2,2,1);//������   order 0����ԭ��״̬������1��ȫ����������
	flash_1(1,10,1);//ѭ������
	flash_2(5,19,0);//������Ч����
	Delay_ms(1000);
	flash_1(1,20,1);//ѭ������
	Delay_ms(500);
	flash_3(2,2,1);//������   order 0����ԭ��״̬������1��ȫ����������
	Delay_ms(100);
	flash_2(10,10,0);//������Ч����
	Delay_ms(100);
	flash_4(10,10,0);//����
	
	
	flash_set(DIG_CODE_0,0,0);
	flash_4(5,10,0);//����
	flash_5(5,10,0);//����
	flash_3(100,1,1);//������  order 0����ԭ��״̬������1��ȫ����������

	
	
	
}


//��Ч2
u8 fl5_ray_state;
int fl5_x,fl5_y;
void flash_5(int time,int speed,int order){
	
	if(order == 0){
			for( fl5_x = 0;fl5_x < time * 8 ;fl5_x++){
				for( fl5_y = 0;fl5_y < speed;fl5_y++){
					
					fl5_ray_state +=1;
					if(fl5_ray_state > speed){
						fl5_ray_state = 0;
						
						
						
					}
					
					lightT = 30;
					flash_toshow(DIG_CODE_0);
					
				}
			}
	}

	
}




//��Чһ��
u8 fl4_ray1,fl4_ray_state;
int fl4_x,fl4_y;
void flash_4(int time,int speed,int order){
	
	if(order == 0){
			for( fl4_x = 0;fl4_x < time * 8 ;fl4_x++){
				for( fl4_y = 0;fl4_y < speed;fl4_y++){
					
					fl4_ray_state +=1;
					if(fl4_ray_state > speed){
						fl4_ray_state = 0;
						
						
						if(fl4_ray1 == 0){
							DIG_CODE_0[0] = 1;
							DIG_CODE_0[7] = 128;
							fl4_ray1 = 1;
						}
						DIG_CODE_0[0] = DIG_CODE_0[0] << 1;
						DIG_CODE_0[0] += 1;
						DIG_CODE_0[7] = DIG_CODE_0[7] >> 1;
						DIG_CODE_0[7] += 128;
						fl4_ray1 += 1;
						if(fl4_ray1 == 8){
							fl4_ray1 = 0;
						}
						
						
						
					}
					
					lightT = 30;
					flash_toshow(DIG_CODE_0);
					
				}
			}
	}

	
}






//���Թ������Ƿ�������(������ȫ      ������ ��
u8 fl3_light,fl3_lnum,fl3_lnum_state,fl3_line,fl3_num;
u16 fl3_x,fl3_y;
//order  0����ԭ��״̬������1��ȫ����������
void flash_3(int time,int speed,int order){
	
	
	if(order == 1){
		for(fl3_num=0;fl3_num<64;fl3_num++)
		{
				DIG_CODE_0[fl3_num] = 0xff;
		}
	}

	fl3_line = 0;fl3_lnum = 0;fl3_lnum_state = 0;
	for( fl3_x = 0;fl3_x < time * 150;fl3_x++){
		for( fl3_y = 0;fl3_y < speed;fl3_y++){
			
	
			
			fl3_line += 1;
			
			if(fl3_line > speed){
				
				fl3_line = 0;
				
				if(fl3_lnum_state == 0){
					
					if(fl3_lnum == 0){
						fl3_lnum_state = 1;
					}else{
						fl3_lnum -=1;
						if(fl3_lnum < 2){
							fl3_lnum_state = 1;
						}
					}
					
				}else if(fl3_lnum_state == 1){
					fl3_lnum +=1;
					if(fl3_lnum > 50){
						fl3_lnum_state = 0;
					}
				}
			}
			
			if(fl3_lnum <=1){
				lightT = 1;
				flash_toshow(DIG_CODE_0);
				
			}else{
				lightT = fl3_lnum;
				flash_toshow(DIG_CODE_0);
				
			}
			
			
			
			
		}
	}		
}




//����Ĳ�֪��ɶ��Ч
u8 num,line, ping,weis,weisi,asnum;
u16 fl2_x,fl2_y;
void flash_2(int time,int speed,int order){
	if(order == 0){
		for( fl2_x = 0;fl2_x < time * 10 ;fl2_x++){
			for( fl2_y = 0;fl2_y < speed;fl2_y++){
			
	
				line += 1;
				if(line > speed){
					
						for(num=0;num<3;num++)
						{
							int a = rand();
							a = a%64;
							
							ping = a/8;
							weis = a%8;
							
							asnum = 1;
							for(weisi=0;weisi<weis;weisi++)
							{
								asnum = asnum * 2;
							}
							DIG_CODE_0[55 + num + ping] = asnum;
							
						}
						
						
						for(num=0;num<5;num++)
						{
							int a = rand();
							a = a%64;
							ping = a/8;
							weis = a%8;
							asnum = 1;
							for(weisi=0;weisi<weis;weisi++)
							{
								asnum = asnum * 2;
							}
							DIG_CODE_0[55 + num + ping] = 0;
						}
						
					line = 0;
					for(num=0;num<55;num++)
					{
							DIG_CODE_0[num] = DIG_CODE_0[num + 8];
					}
				}
				
				lightT = 30;
				flash_toshow(DIG_CODE_0);
				
	
			}
		}
	}
	
		
	
	
	
}




	





unsigned char code DIG_CODE_1[64]={
0x00, 0x00, 0xFE, 0x80, 0x80, 0x80, 0x00, 0x00, 
0x00, 0x0C, 0x92, 0x92, 0x92, 0x7C, 0x00, 0x00, 
0x00, 0x00, 0x9C, 0x92, 0x92, 0xF2, 0x00, 0x00, 
0x00, 0x08, 0x08, 0xFE, 0x48, 0x28, 0x18, 0x00, 
0x00, 0x00, 0xFE, 0x92, 0x92, 0x92, 0x00, 0x00, 
0x00, 0x00, 0xF2, 0x92, 0x92, 0x9E, 0x00, 0x00, 
0x00, 0x02, 0x02, 0xFE, 0x42, 0x02, 0x00, 0x00, 
0x00, 0x30, 0x48, 0x44, 0x22, 0x44, 0x48, 0x30, 
};
//ѭ����ʾ��������ݡ�
u8 fl1_x,fl1_y;
void flash_1(int time,int speed,int order){
	
	if(order == 1){
		for( fl1_x = 0;fl1_x < time * 64;fl1_x++){
			for( fl1_y = 0;fl1_y < speed;fl1_y++){
				
				
				
				GPIO_Fx = 0x00;
				for(f=0;f<8;f++)
				{
				
					GPIO_csx = 0x01;
					for(d=0;d<8;d++)
					{
						GPIO_Dx = DIG_CODE_1[t2 * 8 + d];
			//			GPIO_Dx = 0xff;
						GPIO_csx = GPIO_csx << 1;
					}

					GPIO_Fx = GPIO_Fx_data;
					//GPIO_Fx_data = GPIO_Fx_data << 1;


					Delay(16);
			//		delay(50000);
					GPIO_Fx = 0x00;
					
				}
				t1 += 1;
				if(t1>speed){
					
					t1 = 0;
					GPIO_Fx_data = GPIO_Fx_data << 1;
					if(GPIO_Fx_data < 1){
						
						t2 += 1;
						GPIO_Fx_data = 0x01;
						
						if(t2>=7){
							t2 = 0;
						}
					}
					
				}
				
			}
		}
		
		
		
	}
	
	
}





//ˢ���������������
//DIG_CODE_DATA ����64�����飬����ֵ��1-50��

void flash_toshow(char DIG_CODE_DATA[]){
	
	GPIO_Fx = 0x00;
	GPIO_Fx_data = 0x01;
	for(f=0;f<8;f++)
	{
		GPIO_csx = 0x01;
		for(d=0;d<8;d++)
		{
			GPIO_Dx = DIG_CODE_DATA[f * 8 + d];
			GPIO_csx = GPIO_csx << 1;
		}
		GPIO_Fx = GPIO_Fx_data;
		GPIO_Fx_data = GPIO_Fx_data << 1;
		Delay(lightT);
		GPIO_Fx = 0x00;
		Delay(51 - lightT);
	}
	
}

void flash_set(char DIG_CODE_DATA[],int state,int layer){
	
	
	if(layer == 0){
		if(state == 1){
			for(f=0;f<64;f++)
			{
					DIG_CODE_DATA[f] = 0xff;
			}
		}else if(state == 0){
			for(f=0;f<64;f++)
			{
					DIG_CODE_DATA[f] = 0x00;
			}
		}
	}else{
		if(state == 1){
			for(f=layer * 8;f<layer * 8 + 8;f++)
			{
					DIG_CODE_DATA[f] = 0xff;
			}
		}else if(state == 0){
			for(f=layer * 8;f<layer * 8 + 8;f++)
			{
					DIG_CODE_DATA[f] = 0x00;
			}
		}
	}
	
	
	
	
}





//****************************************************
//MS��ʱ����(12M�����²���)
//****************************************************
void Delay_ms(unsigned int n)
{
	unsigned int  i,j;
	for(i=0;i<n;i++)
		for(j=0;j<123;j++);
}

//****************************************************
//������ʱ����(12M�����²���)
//****************************************************
void Delay(unsigned int n)
{
	unsigned int  i;
	for(i=0;i<n;i++);
}

